﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using EShop.BLL;
using EShop.Common;
using EShop.Model;

namespace EShop.Client
{
    public partial class frmMerchandiseBindEdit : frmBase
    {
        public frmMerchandiseBindEdit(string BindID)
        {
            InitializeComponent();
            this.BindID = BindID;
        }

        private string BindID = string.Empty;

        private bool IsChanged = false;

        private MerchandiseBindBLL oMerchandiseBindBLL = new MerchandiseBindBLL();
        
        private AsyncMethod.ControlsEndueValueEventHandler OnControlEndureValue = null;

        private void frmMerchandiseBindEdit_Load(object sender, EventArgs e)
        {
            IsChanged = false;
            lblBindID.Text = BindID;

            Thread LoadUnitThread = new Thread(new ThreadStart(BindUnitName));
            LoadUnitThread.IsBackground = true;
            LoadUnitThread.Priority = ThreadPriority.Highest;
            LoadUnitThread.Start();
        }

        private void BindUnitName()
        {
            List<MerchandiseUnitInfo> oMerchandiseUnitInfo = new MerchandiseUnitBLL().GetAllMerchandiseUnitList();
            foreach (MerchandiseUnitInfo item in oMerchandiseUnitInfo)
            {
                AsyncMethod.ControlsEndueValueEventHandler OnSet = new AsyncMethod.ControlsEndueValueEventHandler(this.OnComboBoxEndueValueEvent);
                this.BeginInvoke(OnSet, new object(), new AsyncMethod.EndueValueEventArgs(cbbUnit, item, OnSet));
            }
            try
            {
                AsyncMethod.ControlsEndueValueEventHandler OnSetIndex = new AsyncMethod.ControlsEndueValueEventHandler(base.OnComboBoxEndueValueEvent);
                base.BeginInvoke(OnSetIndex, new object(), new AsyncMethod.EndueValueEventArgs(cbbUnit, 0, OnSetIndex));
            }
            catch { log.Write("商品单位为0"); }
            Thread.Sleep(300);
            Thread LoadDataThread = new Thread(new ThreadStart(LoadData));
            LoadDataThread.IsBackground = true;
            LoadDataThread.Start();
        }
        protected override void OnComboBoxEndueValueEvent(object sender, AsyncMethod.EndueValueEventArgs e)
        {
            ComboBox cbbTmp = (ComboBox)e.ControlName;
            if (cbbTmp.InvokeRequired) this.BeginInvoke(e.OnControlEndueValue, new object(), e);
            else cbbTmp.Items.Add(e.SetValue);
        }

        private void LoadData()
        {
            DataTable dt = oMerchandiseBindBLL.MerchandiseBindQuery(BindID);
            string strBindName = dt.Rows[0]["BindName"].ToString();
            string strUnitName = dt.Rows[0]["UnitName"].ToString();
            string strSellPrice = dt.Rows[0]["SellPrice"].ToString();
            OnControlEndureValue = new AsyncMethod.ControlsEndueValueEventHandler(OnTextBoxEndueValueEvent);
            this.BeginInvoke(OnControlEndureValue, new object(),
                new AsyncMethod.EndueValueEventArgs(txtBindName, strBindName, OnControlEndureValue));
            this.BeginInvoke(OnControlEndureValue, new object(),
                new AsyncMethod.EndueValueEventArgs(txtSellPrice, strSellPrice, OnControlEndureValue));

            int index=-1;
            for (int i = 0; i < cbbUnit.Items.Count; i++)
            {
                MerchandiseUnitInfo info = cbbUnit.Items[i] as MerchandiseUnitInfo;
                if (info.UnitName.Equals(strUnitName))
                {
                    index = i;
                    break;
                }
            }

            if (index != -1)
            {
                OnControlEndureValue = new AsyncMethod.ControlsEndueValueEventHandler(base.OnComboBoxEndueValueEvent);
                this.BeginInvoke(OnControlEndureValue, new object(),
                    new AsyncMethod.EndueValueEventArgs(cbbUnit, index, OnControlEndureValue));
            }
            Thread.Sleep(1000);
            IsChanged = false;

        }

        protected override void OnLableEndueValueEvent(object sender, AsyncMethod.EndueValueEventArgs e)
        {
            Label lblTmp = (Label)e.ControlName;
            if (lblTmp.InvokeRequired) this.BeginInvoke(e.OnControlEndueValue, sender, e);
            else
            {
                lblTmp.Text = e.SetValue.ToString();
                lblTmp.Visible = e.Visibile;
            }
        }

        private void btnComplete_Click(object sender, EventArgs e)
        {
            if (IsChanged)
            {
                if (MessageBox.Show("您已经对内容进行了修改，是否需要同步到数据库？", "温馨提示", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    btnUpdate_Click(sender, e);
                }
                else
                {
                    this.DialogResult = DialogResult.Cancel;
                }
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (IsChanged == false) btnComplete_Click(sender, e);
            else
            {
                OnControlEndureValue = new AsyncMethod.ControlsEndueValueEventHandler(this.OnLableEndueValueEvent);
                AsyncMethod.EndueValueEventArgs eArgs = new AsyncMethod.EndueValueEventArgs(lblMsg, "请输入套装的名称", OnControlEndureValue);
                eArgs.Visibile = false;
                this.BeginInvoke(OnControlEndureValue, new object(), eArgs);

                string strBindName = txtBindName.Text.Trim();
                string strSellPrice = txtSellPrice.Text.Trim();
                int UintID = ((MerchandiseUnitInfo)cbbUnit.SelectedItem).UnitID;

                if (string.IsNullOrEmpty(strBindName))
                {
                    eArgs = new AsyncMethod.EndueValueEventArgs(lblMsg, "请输入套装的名称", OnControlEndureValue);
                    eArgs.Visibile = true;
                    this.BeginInvoke(OnControlEndureValue, new object(), eArgs);
                    this.txtBindName.SelectAll();
                    this.txtBindName.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(strSellPrice) || Convert.ToDecimal(strSellPrice) <= 0)
                {
                    eArgs = new AsyncMethod.EndueValueEventArgs(lblMsg, "请输入销售价格,请价格大于0", OnControlEndureValue);
                    eArgs.Visibile = true;
                    this.BeginInvoke(OnControlEndureValue, new object(), eArgs);
                    this.txtSellPrice.Focus();
                    this.txtSellPrice.SelectAll();
                    return;
                }
                if (oMerchandiseBindBLL.MerchandiseBindUpdate(BindID, strBindName, UintID, Convert.ToDecimal(strSellPrice)))
                {
                    MessageBox.Show("修改成功");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("修改失败，请重试或联络管理员");
                }
            }
        }

        private void txtBindName_TextChanged(object sender, EventArgs e)
        {
            IsChanged = true;
        }

        private void cbbUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            IsChanged = true;
        }

        private void txtSellPrice_TextChanged(object sender, EventArgs e)
        {
            IsChanged = true;
        }

        private void txtSellPrice_Leave(object sender, EventArgs e)
        {
            try
            {
                decimal m = Convert.ToDecimal(txtSellPrice.Text.Trim());
            }
            catch
            {
                txtSellPrice.Text = Common.FormatManager.FormatDecimal(0m);
            }
        }
    }
}
